<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Ad extends Model
{
    protected $table = 'ads';
    
    protected $fillable = [
        'title', 'image', 'url', 'position', 'display_order', 
        'status', 'start_time', 'end_time', 'click_count', 'view_count'
    ];
    
    public $timestamps = false;
    
    protected $primaryKey = 'id';
    
    // 广告位置常量
    const POSITION_HOME = 1;      // 首页
    const POSITION_ROOM = 2;      // 房间内
    const POSITION_RESULT = 3;    // 结算页面
    const POSITION_PROFILE = 4;   // 个人中心
    
    // 广告状态常量
    const STATUS_ENABLED = 1;     // 启用
    const STATUS_DISABLED = 2;    // 禁用
    
    /**
     * 获取广告位置名称
     */
    public function getPositionName()
    {
        $positions = [
            self::POSITION_HOME => '首页',
            self::POSITION_ROOM => '房间内',
            self::POSITION_RESULT => '结算页面',
            self::POSITION_PROFILE => '个人中心'
        ];
        
        return $positions[$this->position] ?? '未知';
    }
    
    /**
     * 增加浏览次数
     */
    public function incrementViewCount()
    {
        $this->view_count++;
        $this->save();
    }
    
    /**
     * 增加点击次数
     */
    public function incrementClickCount()
    {
        $this->click_count++;
        $this->save();
    }
    
    /**
     * 检查广告是否有效
     */
    public function isValid()
    {
        $now = time();
        return $this->status == self::STATUS_ENABLED && 
               $now >= $this->start_time && 
               ($this->end_time == 0 || $now <= $this->end_time);
    }
}